package com.dinggou.controller;

import com.dinggou.common.ResponseCode;
import com.dinggou.common.ServerResponse;
import com.dinggou.entity.Category;
import com.dinggou.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/category")
public class CategoryController {


    @Resource(name="categoryServiceImpl")  //这个注解表示自动注入adminSeerviceImpl业务实现类
    @Autowired      //该注解表示后紧跟的变量进行封装，即自动产生getter和setter
    private CategoryService categoryService;

    /****
     * 教程类别删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ServerResponse deleteByPrimaryKey(Integer id){
        if(categoryService.deleteByPrimaryKey(id)>0){
            return ServerResponse.createBySuccess(1,"删除成功");
        }else{
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"删除数据失败");
        }
    }

    /****
     * 添加
     * @param record
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public ServerResponse insert(Category record){

        Category category = new Category();
        category.setName(record.getName());

        if(categoryService.insert(category)>0){
            return ServerResponse.createBySuccess("添加数据成功",category);
        }else{
            return ServerResponse.createByError();
        }
    }


    /****
     * 多个添加
     * @param record
     * @return
     */
    @RequestMapping("/insertSelective")
    @ResponseBody
    public ServerResponse insertSelective(Category record){
        Category category = new Category();
        category.setName(record.getName());

        if(categoryService.insert(category)>0){
            return ServerResponse.createBySuccess("添加数据成功",category);
        }else{
            return ServerResponse.createByError();
        }
    }

    /****
     * 通过id查询
     * @param id
     * @return
     */
    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ServerResponse selectByPrimaryKey(Integer id){
        Category category =categoryService.selectByPrimaryKey(id);

        if(category != null){
            return ServerResponse.createBySuccess(1,categoryService);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员");
        }
    }


    @RequestMapping("/listall")
    @ResponseBody
    public ServerResponse listall(HttpServletRequest request, HttpServletResponse response){

        List<Category> categories=categoryService.selectAll();
        if(categories.size()>0){
            return ServerResponse.createBySuccess(categories.size(),categories);
        }else{
            return ServerResponse.createByErrorMessage("找不到部门单位信息");
        }
    }

    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public ServerResponse updateByPrimaryKeySelective(Category record){
        Category categories=new Category();
        categories.setId(record.getId());
        categories.setName(record.getName());

        if(categoryService.updateByPrimaryKeySelective(categories)>0){
            return ServerResponse.createBySuccess("更新数据成功",categories);
        }else{
            return ServerResponse.createByErrorMessage("更新数据失败");
        }
    }


    /****
     * updateByPrimaryKey通过id更新
     * @param record
     * @return
     */
    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public ServerResponse updateByPrimaryKey(Category record){
        Category categories=new Category();
        categories.setId(record.getId());
        categories.setName(record.getName());

        if(categoryService.updateByPrimaryKey(categories)>0){
            return ServerResponse.createBySuccess("更新数据成功",categories);
        }else{
            return ServerResponse.createByErrorMessage("更新部门单位数据失败");
        }
    }
}